package com.nannybear;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.nannybear.Communication.Connection;
import com.nannybear.Controller.ViewPuppeteer;
import com.nannybear.Model.BabyState;
import com.nannybear.Model.BabyStateChange;
import com.nannybear.Model.Brightness;
import com.nannybear.Model.BrightnessChange;
import com.nannybear.Model.Humidity;
import com.nannybear.Model.HumidityChange;
import com.nannybear.Model.HumidityThreshold;
import com.nannybear.Model.HumidityThresholdChange;
import com.nannybear.Model.LightThreshold;
import com.nannybear.Model.SoundThreshold;
import com.nannybear.Model.Temperature;
import com.nannybear.Model.TemperatureChange;
import com.nannybear.Model.TemperatureThreshold;
import com.nannybear.Model.TemperatureThresholdChange;
import com.nannybear.View.BabyStateScreen;
import com.nannybear.View.BrightnessScreen;
import com.nannybear.View.HelpScreen;
import com.nannybear.View.HomeScreen;
import com.nannybear.View.HumidityScreen;
import com.nannybear.View.Screen;
import com.nannybear.View.SettingsScreen;
import com.nannybear.View.TempScreen;

/**
 * @brief Main activity
 */
public class MainActivity extends Activity {
    /********** DECLARATIONS **********/
    // Variables
    private static Context context;
   // private String ip = "192.168.42.1";       // RED BRICK
   // private int port = 12345;                   // RED BRICK
    private String ip = "10.2.194.54";           //SERVER PC
    private int port = 4444;                    // SERVER PC

    // View classes
    private Screen currentScreen;
    private HomeScreen homeScreen;
    private TempScreen tempScreen;
    private BrightnessScreen brightnessScreen;
    private HumidityScreen humidityScreen;
    private BabyStateScreen babyStateScreen;
    private SettingsScreen settingsScreen;
    private HelpScreen helpScreen;

    // Controller classes
    private ViewPuppeteer viewPuppeteer;

    // Communication classes
    private Connection connection;

    // Model classes
    private SoundThreshold soundThreshold;
    private LightThreshold lightThreshold;
    private HumidityChange humidityChange;
    private TemperatureChange temperatureChange;
    private BrightnessChange brightnessChange;
    private BabyStateChange babyStateChange;
    private HumidityThresholdChange humidityThresholdChange;
    private TemperatureThresholdChange temperatureThresholdChange;
    private Humidity humidity;
    private Temperature temperature;
    private Brightness brightness;
    private BabyState babyState;
    private HumidityThreshold humidityThreshold;
    private TemperatureThreshold temperatureThreshold;


    /**
     * @brief Initialization of the connection.
     */
    public void initializationConnection(){
        this.connection = new Connection(this, this.ip, this.port);
        connection.start();
        try{
            connection.join();
        }catch (InterruptedException ex){
            Log.e("MainActivity", "Interruption when it try to join to thread of Connection");
        }
    }

    /**
     * @brief Load the activity.
     */
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        try{
           this.context = this;
        	this.setContentView(R.layout.activity_main);

            //Model creation
            this.lightThreshold = new LightThreshold(this);
            this.soundThreshold = new SoundThreshold();
            this.humidityChange = new HumidityChange(this);
            this.temperatureChange = new TemperatureChange(this);
            this.brightnessChange = new BrightnessChange(this);
            this.babyStateChange = new BabyStateChange(this);
            this.humidityThresholdChange = new HumidityThresholdChange(this);
            this.temperatureThresholdChange = new TemperatureThresholdChange(this);
            this.humidity = new Humidity();
            this.temperature = new Temperature();
            this.brightness = new Brightness();
            this.babyState = new BabyState();
            this.humidityThreshold = new HumidityThreshold();
            this.temperatureThreshold = new TemperatureThreshold();

            // Connection creation
            initializationConnection();

        	//View creation
            this.homeScreen = new HomeScreen();
            this.tempScreen = new TempScreen();
            this.brightnessScreen = new BrightnessScreen();
            this.humidityScreen = new HumidityScreen();
            this.babyStateScreen = new BabyStateScreen();
            this.settingsScreen = new SettingsScreen();
            this.helpScreen = new HelpScreen();
            this.currentScreen = this.homeScreen;

            //Controller creation
            this.viewPuppeteer = new ViewPuppeteer(this);

            // Add observer
            this.lightThreshold.addObserver(this.connection.getProxyRobot());
            this.soundThreshold.addObserver(this.connection.getProxyRobot());
            this.temperature.addObserver(this.tempScreen);
            this.temperature.addObserver(this.homeScreen);
            this.humidity.addObserver(this.homeScreen);
            this.humidity.addObserver(this.humidityScreen);
            this.brightness.addObserver(this.homeScreen);
            this.brightness.addObserver(this.brightnessScreen);
            this.babyState.addObserver(this.homeScreen);
            this.babyState.addObserver(this.babyStateScreen);
            this.humidityThreshold.addObserver(this.viewPuppeteer.getCriticalityThresholds());
            this.temperatureThreshold.addObserver(this.viewPuppeteer.getCriticalityThresholds());

        }catch (Exception ex){
            Log.e("MainActivity", "Problem on create : "+ex.getMessage());
        }
    }

    /**
     * @brief Destroy the activity.
     */
    protected void onDestroy(){
        super.onDestroy();
        this.connection.closeConnection();
    }


    /*********** ACCESSORS ***********/
    public void setCurrentScreen(Screen currentScr) {
        this.currentScreen = currentScr;
    }

    public Screen getCurrentScreen() {
        return currentScreen;
    }

    public void setHomeScreen(HomeScreen homeScr){
        this.homeScreen = homeScr;
    }

    public HomeScreen getHomeScreen() {
        return homeScreen;
    }

    public void setTempScreen(TempScreen tempScr) {
        this.tempScreen = tempScr;
    }

    public TempScreen getTempScreen() {
        return tempScreen;
    }

    public void setBrightnessScreen(BrightnessScreen brightnessScr){
        this.brightnessScreen = brightnessScr;
    }

    public BrightnessScreen getBrightnessScreen() {
        return brightnessScreen;
    }

    public void setHumidityScreen(HumidityScreen humidityScr) {
        this.humidityScreen = humidityScr;
    }

    public HumidityScreen getHumidityScreen() {
        return humidityScreen;
    }

    public void setBabyStateScreen(BabyStateScreen babyStateScr) {
        this.babyStateScreen = babyStateScr;
    }

    public BabyStateScreen getBabyStateScreen() {
        return babyStateScreen;
    }

    public void setSettingsScreen(SettingsScreen settingsScr) {
        this.settingsScreen = settingsScr;
    }

    public SettingsScreen getSettingsScreen() {
        return settingsScreen;
    }

    public void setHelpScreen(HelpScreen helpScr) {
        this.helpScreen = helpScr;
    }

    public HelpScreen getHelpScreen() {
        return helpScreen;
    }

    public LightThreshold getLightThreshold() {
        return lightThreshold;
    }

    public SoundThreshold getSoundThreshold() {
        return soundThreshold;
    }

    public TemperatureChange getTemperatureChange() {
        return temperatureChange;
    }

    public Temperature getTemperature() {
        return temperature;
    }

    public HumidityChange getHumidityChange() {
        return humidityChange;
    }

    public BrightnessChange getBrightnessChange() {
        return brightnessChange;
    }

    public BabyStateChange getBabyStateChange() {
        return babyStateChange;
    }

    public HumidityThresholdChange getHumidityThresholdChange() {
        return humidityThresholdChange;
    }

    public TemperatureThresholdChange getTemperatureThresholdChange() {
        return temperatureThresholdChange;
    }

    public Humidity getHumidity() {
        return humidity;
    }

    public Brightness getBrightness() {
        return brightness;
    }

    public BabyState getBabyState() {
        return babyState;
    }

    public HumidityThreshold getHumidityThreshold() {
        return humidityThreshold;
    }

    public TemperatureThreshold getTemperatureThreshold() {
        return temperatureThreshold;
    }
}